package com.bw.a2207a.view

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.view.View.OnClickListener
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.a2207a.Const
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseActivity2
import com.bw.a2207a.databinding.ActivityLoginBinding
import com.bw.a2207a.db.CartDbUtil
import com.bw.a2207a.viewmodel.LoginViewModel
import com.tencent.mm.opensdk.modelmsg.SendAuth


@Route(path = Const.path_login)
class LoginActivity : BaseActivity2<ActivityLoginBinding, LoginViewModel>(
    R.layout.activity_login,
    LoginViewModel()
) {


//    @JvmField
//    @Autowired
//    var key: Int = 0
//
//    @Autowired
//    lateinit var userName: String
//
//    @Autowired
//    lateinit var userName1: String
//
//    @Autowired
//    lateinit var userName2: String

    override fun initData() {
        //监听数据变化
        viewModel.loginLiveData.observe(this) {
            ToastUtils.showShort(it.message)
            if (it.code == 200) {
                //登录成功，保存token至本地
                SPUtils.getInstance().put("token", it.data.token)
                //保存用户信息到数据库
                CartDbUtil.cartDb.getUserDao().insertUser(it.data)
                //跳转到主页
                ARouter.getInstance()
                    .build("/main/MainActivity")//目标页面的path
                    .navigation()
            }
        }
    }


    override fun initView() {
        //注入
//        ARouter.getInstance().inject(this)
//        val userName = intent.getStringExtra("userName")
//        key
//        binding.etUsername.setText(userName)
//        binding.etUsername.setText(userName1)
//        binding.etUsername.setText(userName2)

        val name = intent.getStringExtra("name")
        val pass = intent.getStringExtra("pass")

        if (!TextUtils.isEmpty(name)) {
            binding.etUsername.setText(name)

            //正则表达式
            val rg = "^1[356789]\\d{9}\$"

            if (name?.matches(Regex(rg)) == true) {
                //符合手机号格式
            } else {
                //不是手机号
            }
        }
        if (!TextUtils.isEmpty(pass)) {
            binding.etPassword.setText(pass)
        }


        binding.tvRegister.text = "rrfff"
        binding.btn.isEnabled = true

        binding.btn.setOnClickListener {
            val name = binding.etUsername.text.toString()
            val pass = binding.etPassword.text.toString()

            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)) {
                ToastUtils.showShort("请输入用户名或密码")
                return@setOnClickListener
            }

            //声明参数变量 map
            val map = HashMap<String, String>()
            map.put("username", name)
            map["password"] = pass

            //发起网络请求
            viewModel.login(map)
        }

        binding.tvRegister.setOnClickListener { //跳转到注册页面
            val intent = Intent(this@LoginActivity, RegisterActivity::class.java)
            startActivity(intent)
        }
    }


}